<?php
class ReplyAction extends Action{
    public function checkUser($level){
		if(empty($_SESSION['aid'])) $this->redirect('/Admin/Index/login');
		if(!empty($level) && !in_array($level,$_SESSION['levels'])) $this->error('您没有此权限!.');
	}
	public function index(){
		$this->checkUser('6_1_1');
		$data = M('Reply');
		import("@.ORG.Page");
		$count = $data->count('id');
		$p     = new Page ($count, 20);	
		$list = $data->limit($p->firstRow.','.$p->listRows)->order('artime DESC')->select();
		$this->assign('list',$list);
		$page = $p->show();
		$this->assign("page",$page);
		$this->display();
	}
	public function suggest(){
		$this->checkUser('6_2_1');
		$data = M('Suggest');
		import("@.ORG.Page");
		$count = $data->count('id');
		$p     = new Page ($count, 20);	
		$list = $data->limit($p->firstRow.','.$p->listRows)->order('time DESC')->select();
		$this->assign('list',$list);
		$page = $p->show();
		$this->assign("page",$page);
		$this->display();
	}
	public function delSuggest(){
		$this->checkUser('6_2_3');
		$id = (int)$_GET['id'];
		$data = M('Suggest');
		if(!$data->where("id = $id")->delete()) $this->error('删除失败!!');
		$this->success('删除成功!');
	}
	public function detailSuggest(){
		$this->checkUser('6_2_2');
		$id = (int)$_GET['id'];
		$list = M('Suggest')->where("id = $id")->find();
		$this->assign('list',$list);
		$this->display();
	}
	public function reply(){
		$this->checkUser('6_1_4');
		$id = (int)$_GET['id'];
		$data = M('Reply');
		$list = $data->where("id = $id")->find();
		$this->assign('list',$list);
		$this->display();
	}
	public function amend(){
		$this->checkUser('6_1_4');
		$data = M('Reply');
		$list['id'] = (int)$_POST['id'];
		$list['adname'] = $_SESSION['username'];
		$list['adcontent'] = stripcslashes($_POST['adcontent']);
		$list['adtime'] = $_SERVER['REQUEST_TIME'];
		$list['state'] = '1';
		if(!$data->save($list)) $this->error('提交失败!');
		$this->assign('jumpUrl','__URL__/index/');
		$this->success('提交成功!');
	}
	public function detail(){
		$this->checkUser('6_1_2');
		$id = (int)$_GET['id'];
		$list = M('Reply')->where("id = $id")->find();
		$pid = $list['pid'];
		$news = M('News')->field('id,title')->where("id = $pid")->find();
		$list['id'] = $news['id'];
		$list['title'] = $news['title'];
		$this->assign('list',$list);
		$this->display();
	}
	public function del(){
		$this->checkUser('6_1_5');
		$id = (int)$_GET['id'];
		$data = M('Reply');
		$list = $data->field('arid,pid')->where("id = $id")->find();
		if(!$data->where("id = $id")->delete()) $this->error('删除失败!!');
		$uid = $list['arid'];
		$pid = $list['pid'];
		M('News')->where("id = $pid")->setDec('reply');
		M('User')->where("uid = $uid")->setDec('comments');
		$this->success('删除成功!');
	}
	public function delAll(){
		$this->checkUser('3_2_2');
		if(!count($_POST['xuan']))	$this->error('请选择内容!');
		$id = implode(',',$_POST['xuan']);
		$reply = M('Reply')->where("id in ($id)")->select();
		foreach($reply as $list){
			$uid = $list['arid'];
			M('User')->where("uid = $uid")->setDec('comments');
			$pid = $list['pid'];
			M('News')->where("id = $pid")->setDec('reply');
		}
		M('Reply')->where("id in ($id)")->delete();
		$this->success('删除成功! ^_^');
	}
	public function state(){
		$this->checkUser('6_1_3');
		$list['rid'] = (int)$_GET['rid'];
		$list['state'] = (int)$_GET['state'];
		$data = M('Reply');
		if(!$data->save($list)) $this->error('操作失败!!');
		$this->success('操作成功!');
	}
}
?>